package org.geohunter.activity.osmmap;

import org.andnav.osm.views.overlay.OpenStreetMapViewOverlay;
import org.geohunter.Geocache;
import org.geohunter.activity.main.GuiState;
import org.geohunter.activity.map.CacheHint;
import org.geohunter.activity.map.OverlayManager;

import android.util.Log;

import java.util.ArrayList;

public class OnPinTapListener implements OsmPinOverlay.OnItemTapListener<OsmPinItem> {
    private final CacheHint mCacheHint;
    private final OverlayManager<OpenStreetMapViewOverlay> mOverlayManager;
    private final GuiState mGuiState;
    private final ArrayList<OsmPinItem> mPinItems;
    
    public OnPinTapListener(GuiState guiState, 
            OverlayManager<OpenStreetMapViewOverlay> overlayManager, 
            CacheHint cacheHint,
            ArrayList<OsmPinItem> pinItems) {
        mGuiState = guiState;
        mOverlayManager = overlayManager;
        mCacheHint = cacheHint;
        mPinItems = pinItems;
    }
    
    @Override
    public boolean onItemTap(int index, OsmPinItem item) {
        final Geocache geocache = mPinItems.get(index).mGeocache;
        if (geocache == null) {
            mCacheHint.hide();
            return false;
        }
        Log.d("geohunter", "onItemTap " + geocache.getId() + 
                " lat,lon " + geocache.getLatitude() + ", " + geocache.getLongitude());

        if (mCacheHint.getGeocache() == geocache)
            mCacheHint.toggle();
        else
            mCacheHint.showGeocache(geocache);

        mOverlayManager.setSelectedGeocache(geocache);
        mGuiState.setCurrentGeocache(geocache.getId());
        //mGuiState.showCompass(geocache);
        return true;
    }
}
